package com.exit66.jukebox.servlet.xml;

import java.util.Properties;
import javax.servlet.http.HttpServletRequest;

import com.exit66.jukebox.data.TrackCollection;
import com.exit66.jukebox.util.XmlSegment;

public class SearchServlet extends XmlResponseServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3934772401841260867L;

	protected void buildXml(HttpServletRequest req, XmlSegment xml) {
        TrackCollection list = new TrackCollection();
        Properties row;
        boolean searchArtists = false;
        boolean searchAlbums = false;
        boolean searchTracks = false;
        int start = 0;
        int count = 20;
        
    
		if ((req.getParameter("artists") != null) && (req.getParameter("artists") != "0"))
    		searchArtists = true;
    	if ((req.getParameter("albums") != null) && (req.getParameter("albums") != "0"))
    		searchAlbums = true;
    	if ((req.getParameter("tracks") != null) && (req.getParameter("tracks") != "0"))
    		searchTracks = true;
        if (req.getParameter("start") != null) 
        	start = Integer.parseInt(req.getParameter("start"));
        if (req.getParameter("count") != null) 
        	count = Integer.parseInt(req.getParameter("count"));
        
        list.search(req.getParameter("value"), searchArtists, searchAlbums, searchTracks, start, count);
        
        while (!list.isEOF()) {
            row = list.getFields();
            xml.startNode("track");
            xml.writeNode("trackId", row.getProperty("track_id"));
            xml.writeNode("trackName", row.getProperty("track_name"));
            xml.writeNode("albumName", row.getProperty("album_name"));
            xml.writeNode("artistName", row.getProperty("artist_name"));
            xml.endNode("track");
            list.moveNext();
        }
	}

}
